/**
 * 发送验证码封装的方法，依赖jq，toast_min.js，weui
 */
(function ($,weui){
	var sendCode,count,_this;
	
	_this = this;
	
	sendCode = (function (){
		function sendCode(){
			_this.settings = $.extend({}, _this.defaults);
		}
		
		sendCode.prototype.init_sendCode = function (options){
			//按钮的ele对象
			if(options.buttomEle == null){
				throw new Error("buttomEle 为空!");
			}
			options.buttomEle = $('#'+options.buttomEle);
			
			//按钮的获取电话号码的值的对象
			if(options.telEle == null){
				throw new Error("telEleId 为空!");
			}
			options.telEle = $('#'+options.telEle);
			
			_this.settings = $.extend(_this.settings, options);
		}
		
		sendCode.prototype.start_sendCode = function (url){
			var mobile = _this.settings.telEle.val();
			var testMobile = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
			if(null === mobile || '' === mobile || undefined === mobile || 11 !== mobile.length || !testMobile.test(mobile)){
				$.toast_min("请输入正确手机号码!");
				return;
			}
			if(_this.settings.loading){
				return;
			}
			_this.settings.loading = true;
			var loading = weui.loading('数据处理中!', {
	        	className: 'weuiloading'
		    });
		    setTimeout(function () {
			    loading.hide(function() {
			        weui.alert('短信发送成功，注意查收！（模拟发送）');
			        _this.settings.buttomEle.css('color','#a3a3a3');
			        count();
			     });
			}, 2000);
		}
		
		return sendCode;
	})();
	
	/**
	 * 倒计时
	 */
	count = function count(){
		if('true' === _this.settings.isText){
			_this.settings.buttomEle.text(_this.settings.i+'s');
		}else{
			_this.settings.buttomEle.text('('+_this.settings.i+'s)');
		}
		
		_this.settings.i--;
		if(_this.settings.i === 0){
			_this.settings.i = 60;
			_this.settings.buttomEle.text('获取验证码');
			_this.settings.buttomEle.attr("style","");
			_this.settings.loading = false;
			return;
		}else{ 
			setTimeout(count,1000);
		}	
	}
	
	/**
     * 插件的默认值
     */
	_this.defaults = {
		loading: false,				
    	i: 60
    };
	
	/**
     * 添加到$.fn
     */
    $.extend($.fn,{sendCode:new sendCode()});
})($,weui);